//
//  Room.m
//  Maze1
//
//  Created by Lin Tian on 9/27/11.
//  Copyright 2011 tina. All rights reserved.
//

#import "Room.h"


@implementation Room

@synthesize roomId = _roomId;
@synthesize capacity = _capacity;
@synthesize currentNumPlayers = _currentNumPlayers;
@synthesize numForces = _numForces;
@synthesize mapId = _mapId;
@synthesize owner = builder;
@synthesize otherPlayersList;

-(id)initWithRoomId:(int)roomId
{
    if((self = [super init]))
    {
        self.roomId = roomId;
        self.otherPlayersList = [NSMutableArray array];
        self.mapId = 0;
        self.numForces = 0;
    }
    return self;
}

-(id)initWithRoomId:(int)roomId owner:(Player*)aPlayer
{
    if((self = [super init]))
    {
        self.roomId = roomId;
        builder = aPlayer;
        self.otherPlayersList = [NSMutableArray array];
        self.mapId = 0;
        self.numForces = 0;
    }
    return self;
}

-(id)initWithOwner:(Player*)aPlayer mapId:(unsigned int)map numForces:(unsigned char)num
{
    if((self = [super init]))
    {
        self.roomId = 0;
        builder = aPlayer;
        self.otherPlayersList = [NSMutableArray array];
        self.mapId = map;
        self.numForces = num;
    }
    return self;
}

-(void)joinRoomWithPlayer:(Player*)aPlayer
{
    [self.otherPlayersList addObject:aPlayer];
    aPlayer.room = self;
}

-(Player*)findPlayerWithPlayerId:(unsigned int)playerId
{
    if (self.owner.playerId == playerId) {
        return self.owner;
    }
    
    Player* aPlayer = nil;
    for (int i =0; i < [self.otherPlayersList count]; i++) {
        aPlayer = (Player*)[self.otherPlayersList objectAtIndex:i];
        if (aPlayer.playerId == playerId) {
            break;
        }
    }
    return aPlayer;
}

-(void)dealloc
{
    builder = nil;
    
    //not sure whether to release or not
    
    otherPlayersList = nil;
    
    [super dealloc];
}

@end
